package com.gjw.planeWar;

import com.gjw.cor.ColliderChain;
import com.gjw.style.Style;
import utils.RandomUtil;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author gjw19
 * @date 2019/5/19
 */
public class GameModel {

    // 将gamemodel做成单例
    private static final GameModel INSTANCE = new GameModel();

    private GameModel() {

    }

    public static GameModel getInstance() {
        return INSTANCE;
    }

    // 静态代码块调用初始化方法
    static {
        INSTANCE.init();
    }

    static final Props props = Props.getInstance();

    HeroPlane heroPlane;
    int enemyPlaneNums = 0;
    public List<GameObject> gameObjects = new ArrayList<>();
    ColliderChain chain = ColliderChain.getInstance();


    // 重新开始，初始化参数
    public void init() {
        props.LEVEL = PropertyMgr.getInt("LEVEL");
        props.CHANGE_NUM = PropertyMgr.getInt("CHANGE_NUM");
        props.DOWN_SPEED = PropertyMgr.getInt("DOWN_SPEED");
        props.SCORE = PropertyMgr.getInt("SCORE");
        props.SCORE_DOUBLE = PropertyMgr.getInt("SCORE_DOUBLE");
        props.ENEMY_NUM = PropertyMgr.getInt("ENEMY_NUM");
        props.SHOOT_ENEMY_NUM = PropertyMgr.getInt("SHOOT_ENEMY_NUM");
        props.ADD_LEVEL_NUM = RandomUtil.randomNum(10, 30);
        props.ADD_HP_NUM = RandomUtil.randomNum(30, 50);
        props.ADD_EXPLODE_NUM = RandomUtil.randomNum(40, 60);
        props.ENEMY_PLANE_SIZE = RandomUtil.randomNum(0, 5);
        gameObjects.clear();
        createEnemyPlaneList(props.ENEMY_NUM);
        heroPlane = new HeroPlane(props.GAME_WIDTH / 2 - HeroPlane.WIDTH / 2, props.GAME_HEIGHT - HeroPlane.HEIGHT, HeroDir.STOP);
    }

    // 可以通过直接添加父类GameObject来放入子类
    public void add(GameObject go) {
        this.gameObjects.add(go);
    }


    // 可以通过直接删除父类GameObject来移除子类
    public void remove(GameObject go) {
        this.gameObjects.remove(go);
    }

    public void paint(Graphics g) {
        g.drawImage(Style.getINSTANCE().background, 0, 0, null);
        // 显示文字
        Font font = g.getFont();
        Color color = g.getColor();
        g.setColor(Color.BLUE);
        g.setFont(new Font(null, 0, 20));
        g.drawString("第" + props.LEVEL + "关", 175, 60);
        g.setColor(Color.YELLOW);
        g.setFont(new Font(null, 0, 15));
        g.drawString("得分 " + props.SCORE, 173, 85);
        g.setColor(Color.WHITE);
        g.drawString("HP ", 20, 110);
        g.drawString("敌军数 " + enemyPlaneNums, 20, 130);
        g.drawString("击落敌军数：" + props.SHOOT_ENEMY_NUM, 20, 150);
        g.setColor(Color.RED);

        g.setFont(font);
        g.setColor(color);
        // 如果没有飞机，生成飞机
        if (enemyPlaneNums <= props.ENEMY_PLANE_SIZE) {
            createEnemyPlaneList(props.ENEMY_NUM);
        }


        // 全屏爆炸
//        if (props.isAddExplode()) {
////            explodes.add(new Explode(GAME_WIDTH / 2 - Explode.WDITH / 2, GAME_HEIGHT / 2 - Explode.HEIGHT / 2, this));
////            for (int i = 0; i < explodes.size(); i++) {
////                explodes.get(i).paint(g);
////            }
//            props.SHOOT_ENEMY_NUM += enemyPlaneList.size();
//            enemyPlaneList.clear();
//        }

        // 画所有的gameobject
//        heroPlane.paint(g);
//        for (int i = 0; i < enemyPlaneList.size(); i++) {
//            enemyPlaneList.get(i).paint(g);
//        }
//        for (int i = 0; i < bullets.size(); i++) {
//            bullets.get(i).paint(g);
//        }
        for (int i = 0; i < gameObjects.size(); i++) {
            gameObjects.get(i).paint(g);
        }

////        对每个子弹和敌军飞机进行碰撞检测
//        for (int i = 0; i < bullets.size(); i++) {
//            for (int j = 0; j < enemyPlaneList.size(); j++) {
//                bullets.get(i).collideWidth(enemyPlaneList.get(j));
//            }
//        }
//
//        // 对敌军和英雄飞机进行碰撞检测
//        for (int i = 0; i < enemyPlaneList.size(); i++) {
//            heroPlane.collideWidth(enemyPlaneList.get(i));
//        }
//        // 对敌军飞机进行碰撞检测
//        for (int i = 0; i < enemyPlaneList.size(); i++) {
//            for (int j = 0; j < enemyPlaneList.size(); j++) {
//                enemyPlaneList.get(i).collideWidth(enemyPlaneList.get(j));
//            }
//        }
        for (int i = 0; i < gameObjects.size(); i++) {
            for (int j = i + 1; j < gameObjects.size(); j++) {
                GameObject o1 = gameObjects.get(i);
                GameObject o2 = gameObjects.get(j);
                chain.collide(o1, o2);
            }
        }
    }

    // 创建敌军飞机
    public void createEnemyPlaneList(int num) {
        Random random = new Random();
        for (int i = 0; i < num; i++) {
            new EnemyPlane(random.nextInt(400 - EnemyPlane.getWIDTH()), 50, EnemyDir.STOP, RandomUtil.randomNum(2, 8) + Props.DOWN_SPEED);
        }
    }
}
